//
//  AddNewAddressViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/1.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "AddNewAddressViewController.h"
#import "AddNewAddressCell.h"
#import "AddNewAddressBottomCell.h"
#import "AddNewAddressMiddleCell.h"
#import "GFAddressPicker.h"
@interface AddNewAddressViewController ()<GFAddressPickerDelegate,UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *titlesArray;
@property (nonatomic, strong) GFAddressPicker *pickerView;
@end


@implementation AddNewAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}
-(void)createUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[AddNewAddressCell class] forCellReuseIdentifier:@"AddNewAddressCell"];
    [self.tableView registerClass:[AddNewAddressBottomCell class] forCellReuseIdentifier:@"AddNewAddressBottomCell"];
    [self.tableView registerClass:[AddNewAddressMiddleCell class] forCellReuseIdentifier:@"AddNewAddressMiddleCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}
#pragma mark -tablview的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.titlesArray.count;
    }else if (section == 1){
        return 1;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        AddNewAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddNewAddressCell"];
        cell.labell.text = self.titlesArray[indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        AddNewAddressMiddleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddNewAddressMiddleCell"];
        return cell;
    }else{
        AddNewAddressBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddNewAddressBottomCell"];
        cell.textview.delegate = self;
        return cell;
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 || indexPath.section == 1) {
        return 50.f;
    }else{
        return 100.f;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
    }else if (indexPath.section == 1){
        self.pickerView = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [self.pickerView updateAddressAtProvince:@"河南省" city:@"郑州市" town:@"金水区"];
        self.pickerView.delegate = self;
        self.pickerView.font = [UIFont boldSystemFontOfSize:18];
        [self.view addSubview:self.pickerView];
    }else{
        
    }
}
- (void)GFAddressPickerCancleAction
{
    [self.pickerView removeFromSuperview];
}

- (void)GFAddressPickerWithProvince:(NSString *)province
                               city:(NSString *)city area:(NSString *)area
{
    [self.pickerView removeFromSuperview];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:1];
    AddNewAddressMiddleCell * cell = [self.tableView cellForRowAtIndexPath:indexpath];
    cell.labl.text = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
    [self.tableView reloadData];
    
//    self.lab.text = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
    
    NSLog(@"%@  %@  %@",province,city,area);
}

//textview的代理方法
- (void)textViewDidChange:(UITextView *)textView {
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:1];
    AddNewAddressBottomCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
    if (!textView.text.length) {
        cell.placeholderLabel.alpha = 1;
    } else {
       cell.placeholderLabel.alpha = 0;
    }
}

-(NSArray *)titlesArray{
    if (_titlesArray == nil) {
        _titlesArray = @[@"收货人",@"联系电话"];
    }
    return _titlesArray;
}
@end
